/**
 *    Copyright 2009-2019 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.session;

import org.apache.ibatis.builder.xml.XMLConfigBuilder;
import org.apache.ibatis.exceptions.ExceptionFactory;
import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;

/**
 * 用来构建SqlSessionFactory实例
 * Builds {@link SqlSession} instances.
 *
 * @author Clinton Begin
 */
public class SqlSessionFactoryBuilder {
    /**
     * 基于Mybatis配置文件的字符流来创建一个SqlSessionFactory
     *
     * @param reader Mybatis配置文件对应的字符流
     */
    public SqlSessionFactory build(Reader reader) {
        return build(reader, null, null);
    }

    /**
     * 创建一个SqlSessionFactory
     *
     * @param reader      Mybatis配置文件对应的字符流
     * @param environment 默认的环境ID,具体的environment的配置可以参考Mybatis配置文件内的Environments节点,默认为default
     */
    public SqlSessionFactory build(Reader reader, String environment) {
        return build(reader, environment, null);
    }

    /**
     * 创建一个SqlSessionFactory
     *
     * @param reader     Mybatis配置文件对应的字符流
     * @param properties  指定的mybatis属性配置
     */
    public SqlSessionFactory build(Reader reader, Properties properties) {
        return build(reader, null, properties);
    }

    /**
     * 创建一个SqlSessionFactory
     *
     * @param reader      Mybatis配置文件对应的字符流
     * @param environment 默认的环境ID,具体的environment的配置可以参考Mybatis配置文件内的Environments节点,默认为default
     * @param properties   指定的mybatis属性配置
     */
    public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
        try {
            // 加载MyBatis的配置文件
            XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
            // 解析MyBatis的配置文件并返回Configuration对象
            return build(parser.parse());
        } catch (Exception e) {
            throw ExceptionFactory.wrapException("Error building SqlSession.", e);
        } finally {
            ErrorContext.instance().reset();
            try {
                reader.close();
            } catch (IOException e) {
                // Intentionally ignore. Prefer previous error.
            }
        }
    }

    /**
     * 创建一个SqlSessionFactory
     *
     * @param inputStream Mybatis配置文件对应的字节流
     */
    public SqlSessionFactory build(InputStream inputStream) {
        return build(inputStream, null, null);
    }

    /**
     * 创建一个SqlSessionFactory
     *
     * @param inputStream Mybatis配置文件对应的字节流
     * @param environment 默认的环境ID,具体的environment的配置可以参考Mybatis配置文件内的Environments节点,默认为default
     */
    public SqlSessionFactory build(InputStream inputStream, String environment) {
        return build(inputStream, environment, null);
    }

    /**
     * 创建一个SqlSessionFactory
     *
     * @param inputStream Mybatis配置文件对应的字节流
     * @param properties   指定的mybatis属性配置
     */
    public SqlSessionFactory build(InputStream inputStream, Properties properties) {
        return build(inputStream, null, properties);
    }

    /**
     * 创建一个SqlSessionFactory
     * @see #build(InputStream, String, Properties) 
     * @param inputStream Mybatis配置文件对应的字节流
     * @param environment 默认的环境ID,具体的environment的配置可以参考Mybatis配置文件内的Environments节点,默认为default
     * @param properties  指定的mybatis属性配置
     */
    public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
        try {
            // 构建一个Mybatis 主要配置文件的解析器
            XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
            // 使用默认的JDBC会话工厂
            return build(
                    parser.parse()/*解析Mybatis配置*/
            );
        } catch (Exception e) {
            throw ExceptionFactory.wrapException("Error building SqlSession.", e);
        } finally {
            ErrorContext.instance().reset();
            try {
                inputStream.close();
            } catch (IOException e) {
                // Intentionally ignore. Prefer previous error.
            }
        }
    }

    /**
     * 创建一个SqlSessionFactory
     *
     * @param config 指定的Mybatis配置类
     */
    public SqlSessionFactory build(Configuration config) {
        // 构建默认的SqlSessionFactory实例
        return new DefaultSqlSessionFactory(config);
    }

}
